package ph.com.gs3.formalistics.view.document.fields;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import ph.com.gs3.formalistics.global.constants.Constants;
import ph.com.gs3.formalistics.global.utilities.logging.FLLogger;
import ph.com.gs3.formalistics.model.vo.business.form.FormFieldData;
import ph.com.gs3.formalistics.view.document.abstractfields.GroupTypeFieldView;
import android.content.Context;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;

public class CheckboxGroupField extends GroupTypeFieldView {

	public static final String TAG = CheckboxGroupField.class.getSimpleName();

	private List<CheckBox> groupCheckboxes;

	public CheckboxGroupField(Context context, FormFieldData formField) {
		super(context, formField);
		groupCheckboxes = new ArrayList<>();
		createView();
	}

	private void createView() {

		List<String> options = formField.getOptions();
		LinearLayout checkBoxContainer = new LinearLayout(getContext());

		checkBoxContainer.setPadding(0, 0, GROUP_VIEWS_RIGHT_PADDING, 0);
		checkBoxContainer.setOrientation(LinearLayout.VERTICAL);
		checkBoxContainer.setTag(formField.getId());

		for (int i = 0; i < options.size(); i++) {

			String optionString = options.get(i);

			CheckBox cbField = new CheckBox(getContext());
			cbField.setTag(formField.getId() + "_" + i);
			cbField.setTypeface(Constants.getApplicationInstance().getGlobalTypeface());

			// Check if the option is a value label pair, if it is, set the label as the
			// text
			if (optionString.contains("|")) {
				cbField.setText(optionString.split("\\|")[1]);
			} else {
				cbField.setText(optionString);
			}

			cbField.setEnabled(isEnabled);
			checkBoxContainer.addView(cbField);
			groupCheckboxes.add(cbField);

		}

		setValue(formField.getValue());
		this.addView(checkBoxContainer);

	}

	@Override
	public String getValue() {

		int optionSize = formField.getOptions().size();

		String value = "";

		for (int i = 0; i < optionSize; i++) {
			CheckBox cbField = groupCheckboxes.get(i);

			if (cbField.isChecked()) {
				value += cbField.getText().toString() + MULTI_VALUE_SEPARATOR;
			}
		}

		return value;
	}

	@Override
	public void setValue(String value) {
		if (value == null) {
			return;
		}

		String[] selectedOptionsRaw = value.split(MULTI_VALUE_SEPARATOR_ESCAPED);
		List<String> selectedOptions = new ArrayList<String>(Arrays.asList(selectedOptionsRaw));

		int optionSize = formField.getOptions().size();

		for (int i = 0; i < optionSize; i++) {

			String currentOptionString = formField.getOptions().get(i);
			CheckBox cbField = groupCheckboxes.get(i);

			for (String selectedOption : selectedOptions) {

				if (currentOptionString.contains("|")) {
					String optionValue = currentOptionString.split("\\|")[0];
					if (optionValue.equals(selectedOption.trim())) {
						cbField.setChecked(true);
					}
				} else {
					if (currentOptionString.equals(selectedOption.trim())) {
						cbField.setChecked(true);
					}
				}

			}
		}

	}

	@Override
	protected void initializeChangeListener() {
		FLLogger.w(TAG, "initializeChangeListener not yet implemented");
	}

	@Override
	public void showError(String errorMessage) {

		if (groupCheckboxes.size() > 0) {
			groupCheckboxes.get(0).setError(errorMessage);

			for (int i = 0; i < groupCheckboxes.size(); i++) {
				groupCheckboxes.get(i).setOnCheckedChangeListener(new OnCheckedChangeListener() {
					@Override
					public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
						groupCheckboxes.get(0).setError(null);
						// TODO: add a way to unregister the listeners
					}
				});
			}
		}
	}

}
